<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="计划名称">
              <a-input placeholder="请输入计划名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="计划编号">
              <a-input placeholder="请输入计划编号" v-model="queryParam.bianhao"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="计划年度">
                <j-date placeholder="请选择计划年度"  mode="year" dateFormat="yyyy" v-model="queryParam.jihuaniandu"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button @click="handleSent" type="primary" icon="plus">手动生成保养任务</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button type="primary" icon="download" @click="handleExportXls('gd_baoyang_jihua')">导出</a-button>

      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal"
                     @handleSuperQuery="handleSuperQuery"></j-super-query>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap1"
        :rowClassName="setRowClassName"
        @change="handleTableChange">
        <template slot="name" slot-scope="text,record">
          <a v-html="text" @click="handleDetailDrawer(record)"></a>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleWarning(record)">更新告警状态</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <gd-baoyang-jihua-modal ref="modalForm" @ok="modalFormOk"></gd-baoyang-jihua-modal>
    <GdBaoyangJihuaDrawer ref="modalDrawer" />
  </a-card>

</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import GdBaoyangJihuaModal from './modules/GdBaoyangJihuaModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import GdBaoyangJihuaDrawer from './modules/GdBaoyangJihuaDrawer.vue'
import { deleteAction, getAction, httpAction } from '@api/manage'

export default {
  name: 'GdBaoyangJihuaList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    GdBaoyangJihuaModal,
    GdBaoyangJihuaDrawer
  },
  data() {
    return {
      description: 'gd_baoyang_jihua管理页面',
      // 表头
      columns: [
        {
          title: '计划名称',
          align: 'center',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '计划编号',
          align: 'center',
          dataIndex: 'bianhao'
        },
        {
          title: '计划年度',
          align: 'center',
          dataIndex: 'jihuaniandu',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '保养层级',
          align: 'center',
          dataIndex: 'cengji_dictText'
        },
        {
          title: '保养方式',
          align: 'center',
          dataIndex: 'fangshi_dictText'
        },
        {
          title: '保养日期',
          align: 'center',
          dataIndex: 'riqi',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '过期天数',
          align: 'center',
          dataIndex: 'guoqitianshu'
        },
        {
          title: '间隔时长',
          align: 'center',
          dataIndex: 'jiangeshichang'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'beizhu'
        },
        {
          title: '保养设备',
          align: 'center',
          dataIndex: 'shebei_dictText'
        },
        {
          title: '保养项目',
          align: 'center',
          dataIndex: 'xiangmu_dictText'
        },
        {
          title: '执行状态',
          align: 'center',
          dataIndex: 'zhuangtai'
        },
        {
          title: '负责人',
          align: 'center',
          dataIndex: 'fuzeren_dictText'
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '创建日期',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/gongdan/gdBaoyangJihua/list',
        edit: '/gongdan/gdBaoyangJihua/edit',
        delete: '/gongdan/gdBaoyangJihua/delete',
        deleteBatch: '/gongdan/gdBaoyangJihua/deleteBatch',
        exportXlsUrl: '/gongdan/gdBaoyangJihua/exportXls',
        importExcelUrl: 'gongdan/gdBaoyangJihua/importExcel',
        handleSent: 'gongdan/gdBaoyangJihua/taskSent'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleWarning(record) {
      const that = this;
      httpAction(this.url.edit,  { ...record, warning: "0" }, "PUT").then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          const index = that.dataSource.findIndex(item => item.id === record.id);
          if (index !== -1) {
            // 更新warning字段为0
            that.$set(that.dataSource, index, { ...record, warning: "0" });
          }
          // 2. 如果需要触发表格重新渲染（Vue的响应性已自动处理）
          that.$emit('ok');
        } else {
          that.$message.warning(res.message);
        }
      })
      this.loadData()
    },
    setRowClassName(record) {
      // 当 warning 和 plan 字段都为1时返回红色样式类
      if (record.warning === "1" && record.plan === "1") {
        return 'row-red'
      }
      return ''
    },
    handleSent(){
        var ids = "";
        if(this.selectedRowKeys.length <= 0){
          this.$message.warning("请选择一条记录！");
          return;
        }
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认发送",
          content: "是否发送任务？",
          onOk: function () {
            that.loading = true;
            getAction(that.url.handleSent, {ids: ids}).then((res) => {
              if (res.success) {
                //重新计算分页问题
                that.reCalculatePage(that.selectedRowKeys.length)
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
    },
    handleDetailDrawer(record){
        this.$refs.modalDrawer.record=record;
        this.$refs.modalDrawer.visible=true
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'sel_user', value: 'createBy', text: '创建人' })
      fieldList.push({ type: 'date', value: 'createTime', text: '创建日期' })
      fieldList.push({ type: 'string', value: 'name', text: '计划名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bianhao', text: '计划编号', dictCode: '' })
      fieldList.push({ type: 'date', value: 'jihuaniandu', text: '计划年度' })
      fieldList.push({ type: 'int', value: 'cengji', text: '保养层级', dictCode: 'baoyangcengji' })
      fieldList.push({ type: 'int', value: 'fangshi', text: '保养方式', dictCode: 'baoyangfangshi' })
      fieldList.push({ type: 'date', value: 'riqi', text: '保养日期' })
      fieldList.push({ type: 'int', value: 'guoqitianshu', text: '过期天数', dictCode: '' })
      fieldList.push({ type: 'int', value: 'jiangeshichang', text: '间隔时长', dictCode: '' })
      fieldList.push({ type: 'Text', value: 'beizhu', text: '备注', dictCode: '' })
      fieldList.push({
        type: 'popup',
        value: 'shebei_dictText',
        text: '保养设备',
        popup: { code: 'ene_device', field: 'device_name', orgFields: 'device_name', destFields: 'device_name' }
      })
      fieldList.push({
        type: 'popup',
        value: 'xiangmu_dictText',
        text: '保养设备',
        popup: { code: 'gd_baoyang_xiangmu', field: 'xiangmuname', orgFields: 'xiangmuname', destFields: 'xiangmuname' }
      })
      // fieldList.push({ type: 'string', value: 'xiangmu', text: '保养项目', dictCode: '' })
      fieldList.push({ type: 'int', value: 'zhuangtai', text: '执行状态', dictCode: '' })
      fieldList.push({ type: 'string', value: 'fuzeren', text: '负责人', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
::v-deep .row-red {
  background-color: #ffe6e6 !important;
  color: #ff4d4f !important;
}
</style>